package com.qf.blog.commons.utils;

import org.aspectj.weaver.ast.Var;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * User: key
 * ClassName:CookieUtil
 * Date: 2023/6/7
 * Time: 17:23
 * description:
 */
public class CookieUtil {
    public static String getValue(String key){
        if (ObjectUtils.isEmpty(key)) {
            return null;
        }
        HttpServletRequest request = HttpUtils.getRequest();
        Cookie[] cookies = request.getCookies();
        if(cookies!=null&&cookies.length>0){
            for (int i = 0; i < cookies.length; i++) {
                Cookie cookie = cookies[i];
                if(cookie.getName().equals(key)){
                    return cookie.getValue();
                }
            }
        }
        return null;
    }
    public static Cookie getCookie(String key, String value, int maxAge) {
        Cookie cookie = new Cookie(key, value);
        cookie.setPath("/");
        cookie.setMaxAge(maxAge);
        return cookie;
    }
}
